import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import { getUserinfo } from 'network/user'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    token: '',
    userInfo: {}
  },
  mutations: {
    updateToken (state, token) {
      state.token = token
    },
    updateUserInfo (state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    getToken ({ commit }, token) {
      commit('updateToken', token)
    },
    async getUserInfo ({ commit }) {
      const { data: res } = await getUserinfo()
      commit('updateUserInfo', res.data)
    }
  },
  modules: {
  }
})
